package com.shanzmoo.db.report.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.shanzmoo.base.bean.PageRespBase;
import com.shanzmoo.db.report.entity.DataReportEntity;
import com.shanzmoo.db.report.entity.dto.DataReportQueryDto;
import com.shanzmoo.db.report.entity.vo.DataReportVo;
import com.shanzmoo.db.report.mapper.DataReportMapper;
import com.shanzmoo.db.report.service.IDataReportService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 数据报表 服务实现类
 * </p>
 *
 * @author Gzhao
 * @since 2020-12-22
 */
@Service
public class DataReportServiceImpl extends ServiceImpl<DataReportMapper, DataReportEntity> implements IDataReportService {

    @Value("${report.ip}")
    private String reportIp;
    @Value("${report.path}")
    private String reportPath;
    @Value("${report.end}")
    private String reportEnd;

    @Override
    public PageRespBase<DataReportVo> getPageList(DataReportQueryDto queryDto) {
        Page page = PageHelper.startPage(queryDto.getPageNum(), queryDto.getPageSize());
        List<DataReportEntity> list = baseMapper.selectList(
                new QueryWrapper<DataReportEntity>()
                        .eq("deleted", false)
                        .eq("type", queryDto.getType())
                        .like(StrUtil.isNotBlank(queryDto.getReportName()), "report_name", queryDto.getReportName())
        );


        List<DataReportVo> reList = list.stream().map(e -> {
            String url = reportIp + reportPath + e.getType() + "/" + e.getReportName() + reportEnd;
            return DataReportVo.create(e, url);
        }).collect(Collectors.toList());

        PageRespBase<DataReportVo> re = PageRespBase.create(page, reList);
        PageHelper.clearPage();

        return re;
    }
}
